﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace DSCop.Core
{
    public static class DSCopUtil
    {
        public static string GetJobName(XElement job)
        {
            XAttribute nameAttribute = job.Attribute("Identifier");
            if (nameAttribute == null)
            {
                throw new InvalidJobXmlException("Missing job name");
            }

            return nameAttribute.Value;
        }

        public static IList<XElement> GetStages(XElement job)
        {
            IList<XElement> stages = (from c in job.Elements()
                                      where c.Attributes("Type").ToList<XAttribute>().Count != 0
                                        && c.Attributes("Type").ToList<XAttribute>()[0].Value.ToUpper() == "CUSTOMSTAGE"
                                      select c).ToList<XElement>();

            return stages;
        }

        public static IList<XElement> GetStagesByType(XElement job, string stageType)
        {
            IList<XElement> stages = (from c in job.Elements().Elements()
                                      where c.Name.LocalName.ToUpper() == "PROPERTY"
                                        && c.Attribute("Name").Value.ToUpper() == "STAGETYPE"
                                        && c.Value.ToUpper() == stageType.ToUpper()
                                      select c.Parent).ToList<XElement>();

            return stages;
        }

        public static string GetStageName(XElement stage)
        {
            string stageName;

            try
            {
                stageName = (from p in stage.Elements()
                             where p.Attribute("Name").Value.ToUpper() == "NAME" &&
                                 p.Name.LocalName.ToUpper() == "PROPERTY"
                             select p.Value).Single();
            }
            catch (InvalidOperationException ex)
            {
                if (ex.Message == "Sequence contains no elements")
                {
                    throw new InvalidJobXmlException("Stage type element is missing from the XML");
                }

                throw ex;
            }

            return stageName;
        }

        public static string GetStageType(XElement stage)
        {
            string stageType;

            try
            {
                stageType = (from p in stage.Elements()
                             where p.Attribute("Name").Value.ToUpper() == "STAGETYPE" &&
                                 p.Name.LocalName.ToUpper() == "PROPERTY"
                             select p.Value).Single();
            }
            catch (InvalidOperationException ex)
            {
                if (ex.Message == "Sequence contains no elements")
                {
                    throw new InvalidJobXmlException("Stage type element is missing from the XML");
                }

                throw ex;
            }

            return stageType;
        }

        public static string GetStagePropertyValue(XElement stage, string propertyName)
        {
            var stageProperties = stage
                .Descendants("Collection")
                .Descendants("SubRecord");

            foreach (var property in stageProperties)
            {
                string name = (from p in property.Elements()
                               where p.Attribute("Name").Value.ToUpper() == "Name".ToUpper() &&
                                    p.Name.LocalName.ToUpper() == "PROPERTY"
                               select p.Value).Single();

                if (name.ToUpper() == propertyName.ToUpper())
                {
                    string value = (from p in property.Elements()
                                    where p.Attribute("Name").Value.ToUpper() == "VALUE" &&
                                        p.Name.LocalName.ToUpper() == "PROPERTY"
                                    select p.Value).Single();

                    return value;
                }
            }

            throw new InvalidJobXmlException("Invalid property name " + propertyName);
        }
    }
}
